/*
 * 
 */
package com.fipl.parse;
/*
 * This class is used to parsed data for the history page here it take parameter as year, month, day and no of days 
 * for particular variable.
 */
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

public class HistoricalParser {
	
	/*
	 * url1 and url2 will be concatenate with given parameter and form the complete url.
	 */
	private static final String url1 = "http://megan.teksar.com.mx/jsonpreaverage.php?dbname=";
	private static final String url2 = "&action=select&table=daily_average&year=";
	private URL url;										// create url object											
	private HttpURLConnection httpURLConnection=null;		// create http url connection object
	private InputStream inputStream;						// input stream object
	private StringBuffer jsonText = null;					
	public SelectVariableParse selectVariableParse;			// object of select variable parser.

	/*
	 * Historical Parser constructor with parameters database name, year, month, day, no of days and variable
	 */
	public HistoricalParser(String dbName,String year, String month, String day,String noOfDays,String variable){
		selectVariableParse = new SelectVariableParse();	// initialise select variable object.
		try{
			url = new URL("http://megan.teksar.com.mx/DBXTra/index.php/welcome/get_interval_android/"+dbName+"/"+variable+"/"+year+"/"+month+"/"+day+"/"+noOfDays);
//			url = new URL(url1+dbName+url2+year+"&month="+month+"&date="+day+"&nod="+noOfDays+"&field="+variable); // concatenating to form complete URL 
			Log.d("System out", "select variable url "+url.toString());
			httpURLConnection = (HttpURLConnection)url.openConnection();				// will create http connection of given url
			inputStream = httpURLConnection.getInputStream();							// will get stream of data from the http connection.
			BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));	// will store data in buffered reader and will read line by line
			String lineBuffer = null ;													// store data which read line by line
			jsonText = new StringBuffer();
			do{
				lineBuffer = br.readLine();
				jsonText.append(lineBuffer);											// append line by line data in to string buffer
			}while(lineBuffer!=null);
			httpURLConnection.disconnect();												// http connection is closed
			br.close();																	// buffered reader close
		}catch (Exception e){
			// TODO: handle exception
			Log.d("System out", "Error "+e.getMessage());
		}
	}

	public int SelectVariableData(){
		int parsedReturn = 0;						// parsed return is zero if there is no exception
		JSONArray jsonArray;
		try{
			jsonArray = new JSONArray(jsonText.toString());
			for(int i=0; i<jsonArray.length();i++){
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				selectVariableParse.fecha.add(jsonObject.getString("fecha"));			// get data for fecha tag from the JSON
//				selectVariableParse.record.add(jsonObject.getString("record"));			//  get data for record tag from the JSON
				selectVariableParse.average.add(jsonObject.getString("average"));		// get data for average tag from the JSON
			}
		}catch (Exception e) {
			// TODO: handle exception
			parsedReturn = -1;					// parsed return is -1 if there is exception 
		}
		return parsedReturn;
	}
	
}